package com.insight.modules.fileinfo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import com.insight.modules.fileinfo.entity.FileInfo;
import com.insight.modules.fileinfo.mapper.FileInfoMapper;
import com.insight.modules.fileinfo.service.IFileInfoService;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.regex.Pattern;

/**
 * @Description: 文件信息
 * @Author: jeecg-boot
 * @Date:   2021-03-03
 * @Version: V1.0
 */
@Service
public class FileInfoServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo> implements IFileInfoService {

    /**
     * 添加文件返回分组id
     *
     * @param files 文件urls
     * @return
     */
    @Override
    public String getFileGroup(String files) {
        String fileGroup = "";
        //判断文件是否为空
        if(StringUtils.isNotBlank(files)){
            String [] filesArr = files.split(",");
            //添加的文件
            FileInfo fileInfo;
            //文件的分组
            fileGroup = UUID.randomUUID().toString().replace("-", "");
            for(int i =0 ;i<filesArr.length;i++){
                fileInfo = new FileInfo();
                Map<String,String> map = getFileNameAndSuffix(filesArr[i]);
                fileInfo.setFileUrl(filesArr[i]);
                fileInfo.setFileSerial(i);
                fileInfo.setFileGroup(fileGroup);
                fileInfo.setFileName(map.get("fileName"));
                fileInfo.setSuffix(map.get("suffix"));
                this.save(fileInfo);
            }
        }
        return fileGroup;
    }

    /**
     * 批量添加文件
     *
     * @param fileUrls 文件路径
     * @return
     */
    @Override
    public String getFileGroup(List<String> fileUrls) {
        String fileGroup = "";
        if(null != fileUrls && 0 < fileUrls.size()){
            List<FileInfo> lists = new ArrayList<>();
            //添加的文件
            FileInfo fileInfo;
            //文件的分组
            fileGroup = UUID.randomUUID().toString().replace("-", "");
            for (String url:fileUrls){
                fileInfo = new FileInfo();
                Map<String,String> map = getFileNameAndSuffix(url);
                fileInfo.setFileUrl(url);
                fileInfo.setFileSerial(0);
                fileInfo.setFileGroup(fileGroup);
                fileInfo.setFileName(map.get("fileName"));
                fileInfo.setSuffix(map.get("suffix"));
                lists.add(fileInfo);
            }
            this.saveBatch(lists);
        }
        return fileGroup;
    }

    /**
     * 通过分组id获取文件列表
     *
     * @param fileGroup
     * @return
     */
    @Override
    public List<FileInfo> getFilesByFileGroup(String fileGroup) {
        return this.baseMapper.getFilesByFileGroup(fileGroup);
    }

    /**
     * 通过分组id和url 删除文件
     *
     * @param fileGroup
     * @param fileUrls
     */
    @Override
    public void deleteFileGroupAndFileUrl(String fileGroup, List<String> fileUrls) {
        this.baseMapper.deleteFileGroupAndFileUrl(fileGroup,fileUrls);
    }

    /**
     * 批量添加
     *
     * @param fileGroup
     * @param fileUrls
     */
    @Override
    public void addFileGroupAndFileUrl(String fileGroup, List<String> fileUrls) {
        //添加的文件
        FileInfo fileInfo;
        for (String fileUrl :fileUrls ) {
            Map<String,String> map = getFileNameAndSuffix(fileUrl);
            fileInfo = new FileInfo();
            fileInfo.setFileUrl(fileUrl);
            fileInfo.setFileGroup(fileGroup);
            fileInfo.setFileName(map.get("fileName"));
            fileInfo.setSuffix(map.get("suffix"));
            this.save(fileInfo);
        }

    }

    private Map<String,String> getFileNameAndSuffix(String fileUrl){
        Map<String,String> map = new HashMap<>(2);
        String[] splitStr = fileUrl.split(Pattern.quote("/"));
        String fileName = splitStr[splitStr.length-1];
        map.put("fileName",fileName);
        map.put("suffix",fileName.substring(fileName.lastIndexOf('.')+1));
        return map;
    }

    /**
     * 添加单个文件
     *
     * @param fileUrl
     * @return
     */
    @Override
    public String addFileInfo(String fileUrl) {
        if(StringUtils.isNotBlank(fileUrl)){
            FileInfo fileInfo = new FileInfo();
            Map<String,String> map = getFileNameAndSuffix(fileUrl);
            fileInfo.setFileUrl(fileUrl);
            fileInfo.setFileSerial(0);
            fileInfo.setFileName(map.get("fileName"));
            fileInfo.setSuffix(map.get("suffix"));
            this.save(fileInfo);
            return fileInfo.getId();
        }else{
            return  null;
        }
    }
}
